package com.stone.back.dish.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.stone.back.dish.entity.BfCombo;
import com.stone.back.dish.entity.BfComboDish;
import com.stone.back.dish.service.IComboService;
import com.stone.back.user.entity.BfUser;
import com.stone.base.controller.BaseController;
import com.stone.base.entity.Page;

/**
 * 套餐管理
 * @author Stone P
 *
 */
@Controller
@RequestMapping("/backComboCtl")
public class ComboController extends BaseController {
	@Autowired
	private IComboService comboService;
	
	/**
	 * 查询套餐分页信息
	 * @param pageNumber	页数
	 * @param comboChName	中文菜名
	 * @param comboEnName	英文菜名
	 * @param priceMax		单价
	 * @param priceMin		单价	
	 * @param startDate		创建日期
	 * @param endDate		创建日期
	 * @return
	 */
	@RequestMapping("/select")
    public @ResponseBody Page selectComboPage(String pageNumber, String comboChName, String comboEnName, String priceMin, String priceMax, String startDate, String endDate, HttpServletRequest request){
		if(StringUtils.isBlank(pageNumber)) {
			pageNumber = "1";
		}
		Page page = comboService.selectComboPage(Integer.parseInt(pageNumber), comboChName, comboEnName, priceMin, priceMax, startDate, endDate);
		return page;
	}
	
	/**
	 * 查询套餐列表
	 * @return
	 */
	@RequestMapping("/selectcombolist")
    public @ResponseBody List<BfCombo> selectComboList(HttpServletRequest request) {
		List<BfCombo> list = comboService.selectComboList();
		return list;
	}
	
	/**
	 * 增加套餐
	 * @param combo	套餐信息
	 * @return
	 * @throws IOException  
	 */
	@RequestMapping("/operate")
    public ModelAndView operateCombo(BfCombo combo, String dishIds, @RequestParam MultipartFile comboImage, HttpServletRequest request) throws IOException {
		ModelAndView mv = new ModelAndView("/background/combo/combolist");
		if(0 != comboImage.getSize()) {
			String realPath = request.getSession().getServletContext().getRealPath("/upload");
			String fileName = comboImage.getOriginalFilename();
			String fileExtensionName = fileName.substring(fileName.lastIndexOf("."));
			String comboImagePath = realPath + File.separator + System.currentTimeMillis() + fileExtensionName;
			FileUtils.copyInputStreamToFile(comboImage.getInputStream(), new File(comboImagePath));
			String oldFilePath = combo.getPhoto();
			//删除旧图片
			if(StringUtils.isNotBlank(oldFilePath)) {
				oldFilePath = request.getSession().getServletContext().getRealPath(oldFilePath);
				File file = new File(oldFilePath);
				file.delete();
			}
			//修改图片
			combo.setPhoto("/upload/" + comboImagePath.substring(comboImagePath.lastIndexOf(File.separator) + 1));
		}
		//创建信息
		combo.setCreatedate(new Date());
		BfUser currentUser = this.getBackCurrentUser(request);
		combo.setCreateuserid(currentUser.getUserid());
		if(null == combo.getComboid()) {
			comboService.addCombo(combo);
		} else {
			//修改套餐
			comboService.updateCombo(combo);
			//删除原套餐和菜品关系
			comboService.deleteComboDish(combo.getComboid());
		}
		if(StringUtils.isNotBlank(dishIds)) {
			//增加套餐和菜品关系
			String[] dishIdArray = dishIds.split(",");
			for (int i = 0; i < dishIdArray.length; i++) {
				int dishId = Integer.parseInt(dishIdArray[i]);
				BfComboDish comboCombo = new BfComboDish();
				comboCombo.setDishid(dishId);
				comboCombo.setComboid(combo.getComboid());
				comboService.addComboDish(comboCombo);
			}
		}
		return mv;
	}
	
	/**
	 * 获取套餐信息去修改
	 * @param combo	套餐信息
	 * @return
	 * @throws IOException  
	 */
	@RequestMapping("/toUpdate")
    public ModelAndView toUpdateCombo(int comboId, HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/background/combo/comboadd");
		BfCombo combo = this.comboService.selectComboDetail(comboId);
		List<BfComboDish> list = this.comboService.selectComboDishDetail(comboId);
		StringBuilder dishNames = new StringBuilder(); 
		StringBuilder dishIds = new StringBuilder(); 
		for (int i = 0; i < list.size(); i++) {
			BfComboDish comboDish = list.get(i);
			if(i != list.size() - 1) {
				dishIds.append(comboDish.getDish().getDishid()).append(",");
				dishNames.append(comboDish.getDish().getDishchname()).append(",");
			} else {
				dishIds.append(comboDish.getDish().getDishid());
				dishNames.append(comboDish.getDish().getDishchname());
			}
		}
		mv.addObject("combo", combo);
		mv.addObject("dishNames", dishNames);
		mv.addObject("dishIds", dishIds);
		return mv;
	}
	
	/**
	 * 查询套餐详情
	 * @param comboId	套餐Id
	 * @return
	 */
	@RequestMapping("/detail")
    public ModelAndView selectComboDetail(int comboId, HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/background/combo/combodetail");
		BfCombo combo = comboService.selectComboDetail(comboId);
		List<BfComboDish> comboDishList = this.comboService.selectComboDishDetail(comboId);
		mv.addObject("combo", combo);
		mv.addObject("comboDishList", comboDishList);
		return mv;
	}
	
	/**
	 * 删除套餐
	 * @param comboId	套餐Id
	 * @return
	 */
	@RequestMapping("/delete")
    public @ResponseBody boolean deleteCombo(int comboId, HttpServletRequest request){
		BfCombo combo = comboService.selectComboDetail(comboId);
		//删除旧图片
		if(StringUtils.isNotBlank(combo.getPhoto())) {
			String fileRealPath = request.getSession().getServletContext().getRealPath(combo.getPhoto());
			File file = new File(fileRealPath);
			file.delete();
		}
		comboService.deleteCombo(comboId);
		//删除原套餐和菜品关系
		comboService.deleteComboDish(comboId);
		return true;
	}
}
